package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.EmploymentInfo;
import com.project.entity.OrgInfo;
import com.project.entity.StudentInfo;
import com.project.entity.User;
import com.project.myBeans.EmploymentInfoDetail;
import com.project.myBeans.ResponseBean;
import com.project.service.*;
import com.project.service.base.BaseService;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WheelChen on 2017/5/16.
 *
 * 就业信息处理
 */
@Controller
@RequestMapping("/employmentinfo")
public class EmploymentInfoController extends BaseService {
    @Autowired
    EmploymentInfoService employmentInfoService;

    @Autowired
    private StudentInfoService studentInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrgInfoService orgInfoService;


    /**
     * author: WheelChen
     * date: 2017-5-16
     * function: 模糊搜索+条件 分页显示
     *
     * @param employmentInfo
     * @return
     */
    @RequestMapping(value = "/findByCondition", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean findByCondition(@RequestBody EmploymentInfo employmentInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (employmentInfo.getPageIndex() > 0)
                employmentInfo.setPageIndex(employmentInfo.getPageIndex() - 1);
            List<EmploymentInfo> employmentInfos = employmentInfoService.findByCondition(employmentInfo);
            List<EmploymentInfoDetail> employmentInfoDetails = new ArrayList<>();
            for (EmploymentInfo employmentInfo1 : employmentInfos) {
                employmentInfoDetails.add(employmentInfoService.findDetailById(employmentInfo1.getId()));
            }
            responseBean.setData(employmentInfoDetails);
            responseBean.setTotal(employmentInfo.getTotal());
            responseBean.setSuccess(true);
            responseBean.setMessage("查询成功");
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("查询失败");
            return responseBean;
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/25
     * function: 添加信息
     *
     * @param employmentInfo
     * @return
     */
    public ResponseBean add(@RequestBody EmploymentInfo employmentInfo) {
        ResponseBean responseBean = new ResponseBean();
        try {
            //将此学生之前的所有就业信息置为无效
            EmploymentInfo employmentInfo1 = new EmploymentInfo();
            employmentInfo1.setUserid(employmentInfo.getUserid());
            employmentInfo1.setValid(2);//无效
            employmentInfoService.updateBatchByUserId(employmentInfo1);
            //添加新的
            employmentInfoService.add(employmentInfo);
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setUserid(employmentInfo.getUserid());
            studentInfo.setEmployed(2);//已就业
            studentInfoService.modifyByUserIdSelective(studentInfo);
            responseBean.setSuccess(true);
            responseBean.setMessage("添加成功");
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

    @RequestMapping(value = "/findDetailById/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/2
     * function: 按照id查找信息
     *
     * @param id 就业信息表id
     * @return
     */
    public ResponseBean findDetailById(@PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.setData(employmentInfoService.findDetailById(id));
            responseBean.setSuccess(true);
            responseBean.setMessage("查找成功");
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

    @RequestMapping(value = "/findBatchByCondition", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/8/6
     * function: 每一个学生均显示一条就业信息（若无也显示为空）
     *
     * @param studentInfo
     * @return
     */
    public ResponseBean findBatchByCondition(@RequestBody StudentInfo studentInfo){
        ResponseBean responseBean = new ResponseBean();
        try {
            List<JSONObject> jsonObjectList = new ArrayList<>();
            studentInfo.setPageIndex(studentInfo.getPageIndex() - 1);
            List<StudentInfo> studentInfoList = studentInfoService.findBatchByCondition(studentInfo);
            for (StudentInfo studentInfo1 : studentInfoList){
                JSONObject jsonObject = new JSONObject();
                //查询出学生信息详细记录 （用户表 + 学生表 + 系/专业/班级表）
                jsonObject.put("studentInfoDetail",studentInfoService.findDetailByStudentInfo(studentInfo1));
                //就业信息表信息 查询有效的就业信息即valid = 1
                EmploymentInfo employmentInfo = employmentInfoService.findOneByUserAndValid(studentInfo1.getUserid());
                if (employmentInfo != null) {
                    jsonObject.put("employmentInfo", employmentInfo);
                    //单位信息
                    if (employmentInfo.getId() != null) {
                        OrgInfo orgInfo = orgInfoService.findByid(employmentInfo.getEmployerid());
                        jsonObject.put("orgInfo", orgInfo);
                    }
                }
                jsonObjectList.add(jsonObject);
            }
            responseBean.setTotal(studentInfo.getTotal());
            responseBean.setData(jsonObjectList);
            responseBean.setSuccess(true);
            responseBean.setMessage("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;
    }


}
